//
//  DJConnectView.m
//  DualJoy
//
//  Created by lijunge on 2019/11/18.
//  Copyright © 2019 lijunge. All rights reserved.
//

#import "DJConnectView.h"
#import "DJTool.h"
#import "DJConstant.h"
#import "AxcCAAnimation.h"
#import "AxcDrawPath.h"
#import <Masonry.h>

@interface DJConnectView ()

@property (nonatomic, strong) CAShapeLayer *circleShapeLayer;
@property (nonatomic, strong) UIButton *notiseButton;
@property (nonatomic, strong) UIButton *connectButton;
@property (nonatomic, strong) UILabel *statusLabel;
@property (nonatomic, assign) CGRect displayRect;
@property (nonatomic, strong) CAShapeLayer *showLayer;

@end

@implementation DJConnectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}


- (void)layoutSubviews{
    
    [super layoutSubviews];
    self.displayRect = CGRectMake(0, 60, self.bounds.size.width, self.bounds.size.height - 120);
    self.showLayer.frame = self.displayRect;
    [self setupSubViews];
}

- (CAShapeLayer *)showLayer{
    if (!_showLayer) {
        _showLayer = [CAShapeLayer new];
        _showLayer.lineWidth = 1;
        _showLayer.strokeColor = KScienceTechnologyBlue.CGColor;
        _showLayer.fillColor = [UIColor clearColor].CGColor;
        [self.layer addSublayer:_showLayer];
    }
    return _showLayer;
}

- (void)setupSubViews {
    
    [self addSubview:self.notiseButton];
    [self.notiseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(40);
        make.top.equalTo(self.mas_top);
        make.centerX.equalTo(self);
    }];
    
    [self addSubview:self.connectButton];
    [self.connectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(80);
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
    }];
    
    [self addSubview:self.statusLabel];
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo(40);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    self.notiseButton.titleEdgeInsets = UIEdgeInsetsMake(0, -self.notiseButton.imageView.frame.size.width - self.notiseButton.frame.size.width + self.notiseButton.titleLabel.frame.size.width, 0, 0);
    self.notiseButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -self.notiseButton.titleLabel.frame.size.width - self.notiseButton.frame.size.width + self.notiseButton.imageView.frame.size.width);
    
    CGFloat height = self.bounds.size.height;
    CGFloat width = self.bounds.size.width;
    CGPoint arcCenter = CGPointMake(width/2, (height - 120)/2);
    CGFloat arcRadius = (height - 120) / 2;
    UIBezierPath *path = [AxcDrawPath AxcDrawArrowBlockArcRingCenter:arcCenter            // 中心
                                                       outsideRadius:arcRadius            // 外圆半径
                                                         blockRadius:20                   // 箭头半径
                                                          blockCount:6                    // 块个数
                                                        angleSpacing:10                   // 角度间距
                                                          arrowAngle:10                   // 箭头相对伸出角度
                                                          startAngle:-90                  // 起始角度
                                                        openingAngle:0                    // 开合角度
                                                           clockwise:NO];
    self.showLayer.path = path.CGPath;
}

- (void)startAnimation {
    
    CABasicAnimation *animation = [AxcCAAnimation AxcDrawLineDuration:2 timingFunction:kCAMediaTimingFunctionEaseInEaseOut];
    animation.autoreverses = YES;
    animation.repeatCount = HUGE_VALF;
    [self.showLayer addAnimation:animation forKey:@"animation"];
}

- (void)stopAnimation {
    
    [self.showLayer removeAllAnimations];
}

- (void)stopConnect {
    
    self.statusLabel.text = @"REMOTE LOGIN";
    [self stopAnimation];
}

- (void)connectDidSuccess {
 
    self.statusLabel.text = @"REMOTE LOGIN";
    [self stopAnimation];
}

- (void)connectDidFailed {
    
    self.statusLabel.text = @"NOT CONNECTED";
    [self.notiseButton setTitle:@"try again" forState:UIControlStateNormal];
    [self stopAnimation];
}

#pragma mark - Action
- (void)connectButtonTapped:(UIButton *)sender {
    
    self.statusLabel.text = @"CONNECTING...";
    [self startAnimation];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(dj_connectViewStartConnect)]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.delegate dj_connectViewStartConnect];
        });
    }
}

- (void)notiseButtonTapped:(UIButton *)sender {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(dj_connectViewNotiseTapped)]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.delegate dj_connectViewNotiseTapped];
        });
    }
}


- (UIButton *)connectButton {
    
    if (_connectButton == nil) {
        UIButton *connectButton = [[UIButton alloc] init];
        [connectButton setImage:[UIImage imageNamed:@"dianyuan"] forState:UIControlStateNormal];
        [connectButton addTarget:self action:@selector(connectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        _connectButton = connectButton;
    }
    return _connectButton;
}

- (UIButton *)notiseButton {
    
    if (_notiseButton == nil) {
        UIButton *notiseButton = [[UIButton alloc] init];
        notiseButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [notiseButton setImage:[UIImage imageNamed:@"home_notise"] forState:UIControlStateNormal];
        [notiseButton setTitle:@"DUAL IT" forState:UIControlStateNormal];
        [notiseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [notiseButton sizeToFit];
        notiseButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        notiseButton.backgroundColor = [UIColor clearColor];
        //kVCBackColor;
        [notiseButton addTarget:self action:@selector(notiseButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        _notiseButton = notiseButton;
    }
    return _notiseButton;
    
}

- (UILabel *)statusLabel {
    
    if (_statusLabel == nil) {
        UILabel *statusLabel = [[UILabel alloc] init];
        statusLabel.backgroundColor = [UIColor clearColor];
        //kVCBackColor;
        statusLabel.text = @"REMOTE LOGIN";
        statusLabel.font = [UIFont boldSystemFontOfSize:16];
        statusLabel.textAlignment = NSTextAlignmentCenter;
        statusLabel.textColor = [UIColor whiteColor];
        _statusLabel = statusLabel;
    }
    return _statusLabel;
}

@end
